package cn.dansj.repository;

import cn.dansj.entity.InterfaceInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface InterfaceInfoRepository extends JpaRepository<InterfaceInfo, String>, JpaSpecificationExecutor<InterfaceInfo> {
    InterfaceInfo getInterfaceInfoByUrlAndStatusAndMethod(String url, int status, String method);

    InterfaceInfo getBySequence(String sequence);

    InterfaceInfo getByInterfaceName(String interfaceName);

    @Query("select count(t) from InterfaceInfo t where t.url =:url and t.sequence<>:sequence")
    long checkInterfaceUrl(@Param("sequence") String sequence, @Param("url") String url);

    @Query("select count(t) from InterfaceInfo t where t.interfaceName =:name and t.sequence<>:sequence")
    long checkInterfaceName(@Param("sequence") String sequence, @Param("name") String name);

    @Modifying
    @Query(value = "update InterfaceInfo t set t.status=:status where t.sequence=:sequence")
    void updateStatus(@Param("sequence") String sequence, @Param("status") int status);

    @Query(value = "select count(sequence) as num from InterfaceInfo where status = 1 ")
    int countAll();

    @Query(value = "select t from InterfaceInfo t where t.status = 1 ")
    List<InterfaceInfo> getAllInterface();

    @Query("select count(sequence) as num from InterfaceInfo where status = 1 and apiType = 0")
    long countForeignInterface();

    @Modifying
    int deleteBySequence(String sequence);

    @Query("select distinct t.whiteList,t.blackList from InterfaceInfo t where length(t.whiteList) >0 or length(t.blackList)>0")
    List<String[]> getAuthList();
}
